#include <iostream>
#include <string>
#include "commsrv_adapter.hpp"
using namespace std;

commsrv_adapter::commsrv_adapter(){

	memset( server_ip , 0 , MAX_IP_LENGTH );
	server_port  	= 0;

	inited 		= FALSE;
	connected 	= FALSE;
	quit_event	= FALSE;

	producer_p	= NULL;	
}

commsrv_adapter::~commsrv_adapter(){
}

BOOL
commsrv_adapter::initialize( commprd_adapter * prod_p ){
	if( inited ){
		return TRUE;
	}
	if( !set_producer( prod_p ) ){
		throw string("pointer to commprd_adapter is NULL!\n");
	}

	/* do some operations */
	
	inited = TRUE;
	return TRUE;
}

void
commsrv_adapter::uninit(){
	if( !inited ){
		return;
	}
	if( connected ){
		disconnect();
	}

	inited = FALSE;

	/* do some operations */

	return;
}

BOOL
commsrv_adapter::connect(){

	if( !inited ){
		SAY( DP_DEBUG , "commsrv_adapter: Please invoke initialize() before connect() !\n");
		throw string("Please invoke initialize() before connect()");
	}
	
	if( connected ){
		return TRUE;
	}

	/* do some operation */

	connected = TRUE;
	return TRUE;
}

void 
commsrv_adapter::disconnect(){

	if( ! connected ){
		return;
	}
	connected = FALSE;

	/* do some operation */

	return;
}

BOOL
commsrv_adapter::is_server_quit(){

	return quit_event;
}

void
commsrv_adapter::quit(){
	quit_event = TRUE;
	return;
}

BOOL
commsrv_adapter::configure( STUBPARAM params ){

#ifdef _USEDAEMON
	strcpy( server_ip , "127.0.0.1");
#else
	strcpy( server_ip , "192.168.15.241");
#endif
	server_port = 8888;

	if ( server_port <= 0 ) {
		SAY( DP_ERROR, "<ERROR_LOG>no param(xtreamsrv_port)\n" );
		return FALSE;
	}

	return TRUE;
}

BOOL
commsrv_adapter::set_producer( commprd_adapter * prod_p ){

	if( NULL == prod_p ){
		SAY( DP_DEBUG , "pointer to commprd_adapter is NULL !\n");
		return FALSE;
	}

	producer_p = prod_p ;
	return TRUE;
}

